package br.pucrs.prototipoonline.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


/**
 * 
 * @author Ricardo Luckei
 * @author Gelson Gentil
 *
 */
public abstract class POAjaxAction extends POBaseAction {
	
	private Logger logger = Logger.getLogger(this.getClass()); 
	private PrintWriter writer = null;
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		logger.debug("entrando");
		response.setCharacterEncoding("ISO-8859-1");
		usuarioVO = this.getUsuario(request);
		
		try {
			writer = response.getWriter();
		} catch (IOException e1) {
			logger.error(e1);
		}
		
		try {
			logger.debug("chamando metodo abstrato executar()");
			executar(mapping, form, request, response);
			logger.debug("metodo abstrato finalizado");
		} catch(Exception e) {
			e.printStackTrace();
			writer.println("{\"error\": \"true\", \"description\":\""+e.getMessage()+"\"}");
		}
		
		return null;
	}

	protected final void writeOutput(String str) {
		writer.print(str);
	}
	
	public abstract ActionForward executar(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception;
}
